<template>
  <div>
    <!-- 动态绑定数据 -->
    <el-table :data="roleList" border style="width: 100%">
      <!-- 自动循环 添加进去数据 prop -->
      <el-table-column prop="id" label="角色编号" width="180"></el-table-column>
      <el-table-column
        prop="rolename"
        label="角色名称"
        width="180"
      ></el-table-column>
      <el-table-column label="状态" prop="status"> </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="item">
          <div>
            <el-button type="primary" size="mini" @click="edit(item.row.id)">编辑</el-button>
            <el-button
              type="danger"
              size="mini"
              @click="del(item.row.id)"
              icon="el-icon-delete"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { deleteRole,postEditRole } from "../../util/axios";
import { mapGetters, mapActions } from "vuex";
export default {
  data() {
    return {};
  },
  components: {},
  methods: {
    ...mapActions({
      getRoleList: "role/getRoleListAction"
                                               //重改行动名
    }),
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteRole({ id }).then((res) => {
            if (res.data.code == 200) {
                 this.getRoleList();   //传入删除的id后数据库里面就少了对应的数据，前端需要重新调取一遍得到列表的行动，已达到页面更新的效果
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    edit(id){
         this.$emit('edit',id)
    }
  },
  mounted() {
    this. getRoleList(); //触发行动
  },
  computed: {
    ...mapGetters({
      roleList: "role/getRoleList", //最后用这一个渲染
    }),
  },
};
</script>

<style lang="" scoped>
</style>
